home *** CD-ROM | disk | FTP | other *** search
- /*******************************************************************
- *** DirDiff.cmm - Compare directory listings for differences in ***
- *** ver.1 file name, file date, or file size ***
- *******************************************************************/
-
- #include <OptParms.lib>
-
- main(argc,argv)
- {
- RecursiveListing = OptionalParameter(argc,argv,"R");
- if ( ( 1 != argc && 3 != argc )
- || OptionalParameter(argc,argv,"?")
- || OptionalParameter(argc,argv,"?") || OptionalParameter(argc,argv,"HELP") )
- Instructions();
-
- // if directories were not specified, then prompt for them
- if ( 3 == argc ) {
- DirSpec1 = argv[1];
- DirSpec2 = argv[2];
- } else {
- printf("Specify directory listing specification 1: ");
- if ( !(DirSpec1 = gets()) || !DirSpec1[0] )
- Instructions();
- printf("Specify directory listing specification 2: ");
- if ( !(DirSpec2 = gets()) || !DirSpec2[0] )
- Instructions();
- }
-
- // Get sorted list of both directory specifications
- DirList1 = GetDirectoryListing(DirSpec1,RecursiveListing);
- DirList2 = GetDirectoryListing(DirSpec2,RecursiveListing);
-
- // sort each of the listings to speed up bsearch
- qsort(DirList1,"DirectorySortFunction");
- qsort(DirList2,"DirectorySortFunction");
-
- lDifferenceFound = False;
-
- // show files in one directory but not the other
- if ( ShowFilesNotFound(DirSpec1,DirList1,DirSpec2,DirList2) )
- lDifferenceFound = True;
- if ( ShowFilesNotFound(DirSpec2,DirList2,DirSpec1,DirList1) )
- lDifferenceFound = True;
-
- // show files new in one directory than the other
- if ( ShowNewerFiles(DirSpec1,DirList1,DirSpec2,DirList2) )
- lDifferenceFound = True;
- if ( ShowNewerFiles(DirSpec2,DirList2,DirSpec1,DirList1) )
- lDifferenceFound = True;
-
- // show differences in file size
- if ( ShowSizeDifferences(DirSpec1,DirList1,DirSpec2,DirList2) )
- lDifferenceFound = True;
-
- if ( !lDifferenceFound )
- printf("No differences found.\n");
- DirDiffExit( lDifferenceFound ? EXIT_FAILURE : EXIT_SUCCESS );
- }
-
-
- DirDiffExit(ExitCode)
- {
- if defined(_WINDOWS_) {
- printf("\nPress any key to exit...");
- getch();
- }
- exit(ExitCode);
- }
-
- GetDirectoryListing(pSearchSpec,pRecurse)
- {
- // If pSearchSpec is x: or x:\, where X is a drive letter, then just
- // append *.* (* for OS/2) for directory listing
- if ( !strcmp(pSearchSpec+1,":") || !strcmp(pSearchSpec+1,":\\") ) {
- strcat( pSearchSpec, "*.*" );
- } else if ( !strpbrk(pSearchSpec,"?*")
- && Directory(pSearchSpec,False,FATTR_SUBDIR,FATTR_SUBDIR) ) {
- // If specification has no wildcards and is a directory name, then
- // add wildcards
- strcat( pSearchSpec, "\\*.*" );
- } else {
- // If this ends in . or .., possible preceded by : or \, then
- // use it as a directory listing
- if ( !strcmp(".",pSearchSpec) || !strcmp("..",pSearchSpec) )
- strcat( pSearchSpec, "\\*.*" );
- }
-
- lDirList = Directory(pSearchSpec,pRecurse);
-
- if ( !lDirList ) {
- printf("\aNo files found matching \"%s\"\n",pSearchSpec);
- DirDiffExit(EXIT_FAILURE);
- }
-
- // For each entry in DirList, remove the pSearchSpec portion of the name
- pSearchDirLen = strlen(SplitFileName(pSearchSpec).dir);
- for ( lIdx = GetArraySpan(lDirList); 0 <= lIdx; lIdx-- )
- lDirList[lIdx].name += pSearchDirLen;
-
- return lDirList;
- }
-
- DirectorySortFunction(pDirEntry1,pDirEntry2)
- {
- return stricmp(pDirEntry1.name,pDirEntry2.name);
- }
-
- ShowFilesNotFound(pDirSpec,pDirList,pOtherDirSpec,pOtherDirList)
- {
- lDifferenceFound = False;
- lDirCount = 1 + GetArraySpan(pDirList);
-
- for ( lIdx = 0; lIdx < lDirCount; lIdx++ ) {
- if ( -1 == bsearch(pDirList[lIdx],pOtherDirList,"DirectorySortFunction") ) {
- if ( !lDifferenceFound ) {
- lDifferenceFound = True;
- printf("\nFiles in \"%s\", but not \"%s\":\n",pDirSpec,pOtherDirSpec);
- }
- printf("%s\n",pDirList[lIdx].name);
- }
- }
- if ( lDifferenceFound )
- printf("\n");
- return lDifferenceFound;
- }
-
- ShowNewerFiles(pDirSpec,pDirList,pOtherDirSpec,pOtherDirList)
- {
- lDifferenceFound = False;
- lDirCount = 1 + GetArraySpan(pDirList);
-
- for ( lIdx = 0; lIdx < lDirCount; lIdx++ ) {
- if ( -1 != (lOtherIdx=bsearch(pDirList[lIdx],pOtherDirList,"DirectorySortFunction")) ) {
- if ( 0 < difftime(pDirList[lIdx].Write,pOtherDirList[lOtherIdx].Write) ) {
- if ( !lDifferenceFound ) {
- lDifferenceFound = True;
- printf("\nNewer in \"%s\" than \"%s\":\n",pDirSpec,pOtherDirSpec);
- }
- printf("%-40s ",pDirList[lIdx].name);
- PrintDirectoryDate(pDirList[lIdx].Write);
- printf(" : ");
- PrintDirectoryDate(pOtherDirList[lOtherIdx].Write);
- printf("\n");
- }
- }
- }
- if ( lDifferenceFound )
- printf("\n");
- return lDifferenceFound;
- }
-
- PrintDirectoryDate(pTime)
- {
- tm = localtime(pTime);
- printf("%2d-%02d-%02d %2d:%02d%c",
- tm.tm_mon+1,tm.tm_mday,tm.tm_year%100,
- (tm.tm_hour%12) == 0 ? 12 : (tm.tm_hour%12),tm.tm_min,
- tm.tm_hour < 12 ? 'a' : 'p');
- }
-
- ShowSizeDifferences(pDirSpec,pDirList,pOtherDirSpec,pOtherDirList)
- {
- lDifferenceFound = False;
- lDirCount = 1 + GetArraySpan(pDirList);
-
- for ( lIdx = 0; lIdx < lDirCount; lIdx++ ) {
- if ( -1 != (lOtherIdx=bsearch(pDirList[lIdx],pOtherDirList,"DirectorySortFunction")) ) {
- if ( pDirList[lIdx].size != pOtherDirList[lOtherIdx].size ) {
- if ( !lDifferenceFound ) {
- lDifferenceFound = True;
- printf("\nSize differences \"%s\" : \"%s\":\n",pDirSpec,pOtherDirSpec);
- }
- printf("%s %d : %d\n",pDirList[lIdx].name,
- pDirList[lIdx].size,pOtherDirList[lOtherIdx].size);
- }
- }
- }
- if ( lDifferenceFound )
- printf("\n");
- return lDifferenceFound;
- }
-
-
- Instructions()
- {
- printf("\a\n")
- printf("DirDiff.cmm - Show differences in directory listings\n")
- printf("\n")
- printf("USAGE: CEnvi DirDiff.cmm <Dir1> <Dir2>\n")
- printf("\n")
- printf("WHERE: Dir1, Dir2 - Specifications for two directory listings.\n")
- printf("\n")
- printf("NOTE: For current directory use \".\" or \".\*.*\"\n")
- printf("\n")
- printf("EXAMPLES: CEnvi DirDiff c:\Mine d:\Theirs\n")
- printf(" CEnvi DirDiff.cmm . d:..\*.*\n")
- printf("\n")
- DirDiffExit(EXIT_FAILURE);
- }